package newsDemo.service.Impl;

import newsDemo.mapper.agreeMapper;
import newsDemo.mapper.newsMapper;
import newsDemo.pojo.agree;
import newsDemo.pojo.news;
import newsDemo.service.agreeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class agreeServiceImpl implements agreeService {

    @Autowired
    private agreeMapper agreeMapper;

    @Autowired
    private newsMapper newsMapper;

    @Override
    public boolean check(agree agree) {
        agree check = agreeMapper.get(agree);
        if(check == null){
            return false;
        }
        return true;
    }

    @Override
    public int change(agree agree, boolean flag) {
        if(flag == true) {
            agree check = agreeMapper.get(agree);
            if(check == null){
                agreeMapper.add(agree);
                news news = newsMapper.getNewsById(agree.getNewsId());
                if(news == null)  return 0;
                news.setAgreeNumber(news.getAgreeNumber()+1);
                newsMapper.update(news);
                return 0;
            } else {
                return 1;
            }
        } else {
            agree check = agreeMapper.get(agree);
            if(check != null){
                agreeMapper.delete(agree);
                news news = newsMapper.getNewsById(agree.getNewsId());
                if(news == null)  return 0;
                news.setAgreeNumber(news.getAgreeNumber()-1);
                newsMapper.update(news);
                return 0;
            } else {
                return 2;
            }
        }
    }

    @Override
    public List<agree> getAgreeList(int id) {
        return agreeMapper.getAll(id);
    }
}
